package edu.jamestang.yeb.controller;

import edu.jamestang.yeb.entity.Salary;
import edu.jamestang.yeb.service.SalaryService;
import edu.jamestang.yeb.util.Resp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author JamesTang
 * @date 2021/12/29 14:14
 */
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @ApiOperation(value = "Get all Salary")
    @GetMapping("/")
    public List<Salary> getAllSalary(){
        return salaryService.list();
    }

    @ApiOperation(value = "add Salary")
    @PostMapping("/")
    public Resp addSalary(@RequestBody Salary salary){
        salary.setCreateDate(LocalDateTime.now());
        if (salaryService.save(salary)){
            return Resp.success("Add successful");
        }
        return Resp.error("Add fail");
    }

    @ApiOperation(value = "Delete salary")
    @DeleteMapping("/{id}")
    public Resp deleteSalary(@PathVariable Integer id){

        if(salaryService.removeById(id)){
            return Resp.success("Delete successful");
        }
        return Resp.error("Delete failure");
    }

    @ApiOperation(value = "Update salary")
    @PutMapping("/")
    public Resp updateSalary(@RequestBody Salary salary){
        if(salaryService.updateById(salary)){
            return Resp.success("Update successful");
        }
        return Resp.error("Update failure");
    }
}
